<?php
namespace Ekadmin\Controller;
use \Think\Controller;
use App\Config\YlyConfig;
use App\Oauth\YlyOauthClient;
use App\Api\PrintService;
use App\Api\PicturePrintService;
use App\Api\ExpressPrintService;
class PrintController extends CommonController{
    
    public function index(){
        echo date("Y-m-d H:i:s");
    }
    
    public function printop(){
        
        if(!isset($_REQUEST["ordernum"])){
            exit("参数不能为空");
        }
        $out_trade_no=I('ordernum');
        
        if(empty($out_trade_no)){
            exit('订单号不能为空');
        }
        
        Vendor('lib.Autoloader');
        
        Vendor('lib.Config.YlyConfig');
        
        $map['uniacid'] = $this->uniacid;
        
        $data  = M('config')->where($map)->find();
         
        if(empty($data['clientid']) || empty($data['clientsecret']) || empty($data['granttype'])){
            exit("系统打印机设置没有配置");
        }

        $grantType = $data['granttype'];                     //'client_credentials' 自有型应用;
        
        define('GRANTTYPE', $grantType);
        
        $clientId = $data["clientid"];                       //应用id
        
        $clientSecret = $data["clientsecret"];;              //应用密钥
        
        $config=new YlyConfig($clientId, $clientSecret);
        
        
        if (GRANTTYPE == 'client_credentials') {            //'client_credentials' 自有型应用;
            
            $refresh_token =  S('refreshConfig');
            $access_token = S('accessConfig');
        
            if(!$refresh_token)
            {
                $client = new YlyOauthClient($config);
                try {
                    $token = $client->getToken();
                } catch (Exception $e) {
                    echo $e->getMessage() . "\n";
                    print_r(json_decode($e->getMessage(), true));
                    return;
                }
                
                $access_token = $token->access_token;           //调用API凭证AccessToken 永久有效,请妥善保存.
                $refresh_token = $token->refresh_token;         //刷新AccessToken凭证 失效时间35天
                $expires_in = $token->expires_in;               //自有型应用可忽略此回调参数, AccessToken失效时间30天
                
                S('refreshConfig',$refresh_token,259000);
                S('accessConfig',$access_token,259000);
            }
            /*获取订单信息开始*/
            
            
            $rs=M("order")->field("is_use_hb,hbfee,is_use_quan,quyu_id,xiaoqu_id,uid,total_jiner,pay_jiner,address,mobile,name,addtime,paytime,goodsinfo,peisong_type,total_num")->where("out_trade_no='".$out_trade_no."'")->find();
            
            if(!$rs){
                exit("订单不存在");
            }
            
            $res=M("xiaoqu")->find($rs["xiaoqu_id"]);
            
            if(!$res ){
                exit("小区不存在，无法取得打机编码");
            }
            
            if(!$res["machine_code"]){
                exit("小区没有配置打印机");
            }
            
            $quyu=M("quyu")->find($rs["quyu_id"]);
            if(!$quyu){
                exit("线路不存在");
            }
            
//             $rt=M("member_quan")->find($rs["quan_id"]);
            
//             switch ($rt["quan_type"]){
//                 case 1:
//                     $rt["quan_type"]="通用券";
//                     break;
//                 case 2:
//                     $rt["quan_type"]="满减券";
//                     break;
//                 case 3:
//                     $rt["quan_type"]="指定商品";
//             }
            
            $machine_code = $res["machine_code"];                      //原本为空 tangfeng添加    //机器码
            
            $origin_id = $out_trade_no;                    //内部订单号(32位以内)
            /*获取订单信息结束*/
            if (empty($machine_code)) {echo 'The machine_code cannot be empty';return;}
            
            if (empty($origin_id)) {echo 'The origin_id cannot be empty';return;}

            /**文本接口开始**/
            $print = new PrintService($access_token, $config);
            //58mm排版 排版指令详情请看 http://doc2.10ss.net/332006
            $content = "<FS2><center>**#1".$data["ptr_platform"]."打印**</center></FS2>";
            $content .= str_repeat('.', 32);
            $content .= "<FS2><center>--在线支付--</center></FS2>";
            $content .= "<FS><center>".$data["ptr_xcxname"]."</center></FS>";
            $content .= "订单时间:". date("Y-m-d H:i",$rs['addtime']) . "\n";
            $content .= "订单编号:".$origin_id."\n";
            $content .= "线路名称:".$quyu["name"]."\n";
            $content .= "收货小区:".$res["name"]."\n";
            $content .= "团长姓名:".$res["tuan_name"]."\n";
            $content .= "团长手机:".$res["mobile"]."\n";
            $content .= "收货人:".$rs["name"]."\n";
            $content .= "联系电话:".$rs["mobile"]."\n";
            $content .= "收货地址:".$rs["address"]."\n";
            if($res["peisong_type"]==1){
                $content .= "配送方式:自提\n";
            }else{
                $content .= "配送方式:送货上门\n";
            }
                       
            $content .= str_repeat('*', 14) . "商品" . str_repeat("*", 14);
            $content .= "<table>";
            $goodsinfo=json_decode($rs["goodsinfo"],true);
            foreach($goodsinfo as $v){
                $content .= "<tr><td>".$v["name"]."</td><td>x".$v["num"]."</td><td>".$v["tuan_price"]."</td></tr>";
            }
           
            $content .= "</table>";
            $content .= str_repeat('.', 32);
            $content .= "<QR>".$out_trade_no."</QR>";
            $content .= "商品总数:".$rs["total_num"]." \n";
            $content .= "实付金额:￥".$rs["pay_jiner"]."\n";            
            
            if($rs["is_use_quan"]==2){
                $content .= "优惠券金额:".$rt["quan_jiner"]." \n";
            }
            
            if($rs["is_use_hb"]==2){
                $content .= "红包金额:".$rt["hbfee"]." \n";
            }
            
            $content .= str_repeat('*', 32);
            $content .= "总金额:￥".$rs["total_jiner"]."\n";
            $content .= "<FS2><center>**#1 完**</center></FS2>";
            try{
                
                //$this->success("打印成功");
                //U('Order/index');
                var_dump($print->index($machine_code, $content, $origin_id));
            }catch (Exception $e) {
                echo $e->getMessage();
            }
            /**文本接口结束**/            
            
        }
        
        if (GRANTTYPE == 'authorization_code') {
            include_once __DIR__ . '/AuthorizationCodeMode/callback.php';//开放型应用
            
            
        }
        
    }
    
}